import { defineStore } from 'pinia'

const useUser = defineStore('useUser', {
  state: () => ({
    userInfo: JSON.parse(localStorage.getItem('userInfo') || '{}'), // 用户信息
    token: localStorage.getItem('token') || '' // token
  }),
  actions: {
    // 保存用户信息
    saveUserInfo(userInfo: any) {
      this.userInfo = userInfo
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
    },
    // 保存token
    saveToken(token: string) {
      this.token = token
      localStorage.setItem('token', token)
    },
    // 清除用户信息
    clearUserInfo() {
      this.userInfo = {}
      this.token = ''
      localStorage.removeItem('userInfo')
      localStorage.removeItem('token')
    }
  }
})

export default useUser
